﻿using System.Web.UI;
using System.Web.UI.WebControls;

namespace OrbitOne.Elmah.Presenter
{
    public class RequiredFieldValidatorForCheckBoxLists : BaseValidator
    {
        #region Constants
        private const string VALIDATION_JAVASCRIPT = @"function RequiredFieldValidatorForCheckBoxListIsValid(val)
                                       {         
                                          var value = """";
                                        if (typeof(val.controltovalidate) == ""string"") 
                                        {
                                            control = document.getElementById(val.controltovalidate);
                                            inputs = control.getElementsByTagName('input');

                                           var checkboxes = new Array();
                                            for (i = 0; i < inputs.length; i++)
                                            {
                                              if (!inputs[i].length)
                                              {
                                                if (inputs[i].type == 'checkbox')
                                                {
                                                    checkboxes[checkboxes.length] = inputs[i];
                                                }
                                              }
                                              else
                                              {
                                                for(k = 0; k < inputs[i].length; k++)
                                                {
                                                  if (inputs[i][k].type == 'checkbox')
                                                    checkboxes[checkboxes.length] = inputs[i];
                                                }
                                              }
                                            }
                                            for (i = 0; i < checkboxes.length; i++)
                                            {
                                                if(checkboxes[i].checked) return true;
                                            }
                                           
                                        }
                                        
                                        return false;
                                       }";
        #endregion

        #region Private Members
        private ListControl _listctrl;
        #endregion

        #region Methods
        public RequiredFieldValidatorForCheckBoxLists()
        {
            EnableClientScript = false;
        }

        protected override bool ControlPropertiesValid()
        {
            var ctrl = FindControl(ControlToValidate);

            if (ctrl != null)
            {
                _listctrl = (ListControl)ctrl;
                return (_listctrl != null);
            }

            return false;
        }

        protected override bool EvaluateIsValid()
        {
            return _listctrl.SelectedIndex != -1;
        }

        protected override void OnInit(System.EventArgs e)
        {
            base.OnInit(e);
            Page.ClientScript.RegisterClientScriptBlock(GetType(), 
                                                        "ValidationJavaScript", 
                                                        VALIDATION_JAVASCRIPT, 
                                                        true);
        }

        protected override void AddAttributesToRender(HtmlTextWriter writer)
        {
            base.AddAttributesToRender(writer);
            if (RenderUplevel)
            {
                Page.ClientScript.RegisterExpandoAttribute(ClientID,
                                                           "evaluationfunction",
                                                           "RequiredFieldValidatorForCheckBoxListIsValid",
                                                           false);
            }
        }
        #endregion
    }
}
